//
//  LBUtils.cpp
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 11/12/12.
//  Copyright (c) 2012 UIT. All rights reserved.
//

#include <iostream>
#include "LBUtils.h"
#include "LBGraphics.h"
#include "string.h"

bool isExistStringInList(list<const char*>* strings, const char* element){
    list<const char*>::iterator it;
    for(it = strings->begin(); it != strings->end(); it++){
        const char* str = (*it);
        if(strcmp(str, element) == 0){
            return true;
        }
    }
    return false;
}

int indexOfStringInList(list<const char*>* strings, const char* element){
    list<const char*>::iterator it;
    int index = 0;
    for(it = strings->begin(); it != strings->end(); it++){
        const char* str = (*it);
        if(strcmp(str, element) == 0){
            return index;
        }
        index++;
    }
    return -1;
}

void deleteSingleton(){
    LBGraphics::getInstance()->deleteInstance();
}

void createHistogram(unsigned char* imageData, int dataLength, int* outHistogram){
    for (int i = 0; i < 256; i++) {
        outHistogram[i] = 0;
    }
    
    for (int index = 0; index < dataLength; index+= 4) {        
        int red = imageData[index];
        int green = imageData[index + 1];
        int blue = imageData[index + 2];
        
        int v = red * 0.299 + green * 0.587 + blue * 0.114;
        
        outHistogram[v]++;
    }
}